import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import('../views/login/index.vue')
const Layui = () => import('../views/layui/index.vue')
const Article = () => import('../views/articles/index.vue')
const Image = () => import('../views/image/index.vue')
const Comment = () => import('../views/comment/index.vue')
const AddAtricle = () => import('../views/addArticle/index.vue')
const Fans = () => import('../views/fans/index.vue')
const Page = () => import('../views/page404/index.vue')
const EditArticle = () => import('../views/articles/edit.vue')
const Settings = () => import('../views/setting/index.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layui,
    children: [
      {
        path: '/article',
        component: Article,
        name: 'Article'
      },
      {
        path: '/editArticle/:id',
        component: EditArticle,
        name: 'EditArticle'
      },
      {
        path: '/image',
        component: Image,
        name: 'Image'
      },
      {
        path: '/comment',
        component: Comment,
        name: 'Comment'
      },
      {
        path: '/addArticle',
        component: AddAtricle,
        name: 'AddArticle'
      },
      {
        path: '/fans',
        component: Fans,
        name: 'Fans'
      },
      {
        path: '/settings',
        component: Settings,
        name: 'Settings'
      }
    ]
  },
  {
    path: '/login',
    component: Login,
    name: 'Login'
  },
  {
    path: '*',
    component: Page,
    name: 'Page'
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // console.log(to, from, next)
  // 如果你没有登陆，则不让进入 主页
  if (to.path !== '/login') {
    // 检查是否有token
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      // 回登陆页
      alert('没有登录，请登录')
      next('/login')
    }
  } else {
    next()
  }
})
export default router
